using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
///Xmlserdeser  is xml serialize
/// </summary>
public class Xmlserdeser
{
    private string XMLfilename; //.xml
    private string simplifyName; // .xml
    private string JSONfilename; //.JSON
    private string dir;
    private page mpage;

    public page Mpage
    {
        set
        {
            mpage = value;
        }
        get
        {
            return mpage;
        }
    }

    public string XMLFilename
    {
        set
        {
            XMLfilename = value;
        }
        get
        {
            return XMLfilename;
        }
    }
    public string SimplifyName
    {
        set
        {
            simplifyName = value;
        }
        get
        {
            return simplifyName;
        }
    }
    public string Jsonfilename
    {
        set
        {
            JSONfilename = value;
        }
        get
        {
            return JSONfilename;
        }
    }

    public page read()
    {
        if (!File.Exists(XMLfilename))
        {
            return null;
        }
        
        XmlSerializer ser = new XmlSerializer(typeof(page));
        StreamReader reader = new StreamReader(XMLfilename);//the value of "XMLfilename" is "//pageSerialized.xml"
        page obj=new page();
        try
        {
            obj = (page)ser.Deserialize(reader);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            reader.Dispose();
        }
        return obj;
    }


    public void write(page obj,string tmpFilename)
    {
        XmlSerializer ser = new XmlSerializer(typeof(page));
        StreamWriter writer = new StreamWriter(tmpFilename);
        try
        {
            ser.Serialize(writer, obj);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            writer.Dispose();
        }
    }


	public Xmlserdeser()
	{
		
	}

    public Xmlserdeser(string predir, string username,string directory) //if the data file exists, then deserialize it, else serialize the data to a file.
    {
        dir = predir + username;
        if (directory.Length > 0)
        {
            dir=dir+ "/" + directory;
        }
        XMLfilename = dir + "/pageSerialized.xml";
        simplifyName = dir + "/pageSimplified.xml";
        JSONfilename = dir + "/sendData.json";

        if (File.Exists(dir + "/pageSerialized.xml"))
        {
            try
            {
                mpage = read();
            }
            catch (Exception ex)
            {
                LOG.writer(ex.ToString());
                mpage = new page(predir, username);
                write(mpage,XMLfilename);
            }
        }
        else
        {
            mpage = new page(predir, username);
            write(mpage,XMLfilename);
        }        
    }

    public Xmlserdeser(string v)
    {
        XMLfilename = v;
    }

    public string simplifyXmlandJSON()
    {
        mpage.Muser = null;

        write(mpage, simplifyName);

        XmlDocument doc = new XmlDocument();
        doc.Load(simplifyName);
        string JSON = XmlToJSONClass.XmlToJSON(doc);
        File.WriteAllText(JSONfilename, JSON);
        return JSON;
    }

    public static object Clone(object obj)
    {
        if (!obj.GetType().IsSerializable)
        {
            throw new ArgumentException("对象不是可序列化的。");
        }
        IFormatter format = new BinaryFormatter();
        using (Stream stream = new MemoryStream())
        {
            format.Serialize(stream, obj);
            stream.Seek(0, SeekOrigin.Begin);
            return format.Deserialize(stream);
        }

    }  
}
